package com.tsinghua.discharge.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsinghua.discharge.common.Const;
import com.tsinghua.discharge.common.ServerResponse;
import com.tsinghua.discharge.dao.*;
import com.tsinghua.discharge.model.*;
import com.tsinghua.discharge.service.CalALParamService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.swing.text.View;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CalALParamServiceImpl implements CalALParamService {
    @Autowired
    ViewResultMapper viewResultMapper;
    @Autowired
    SectorMapper sectorMapper;
    @Autowired
    RegionMappingMapper regionMappingMapper;
    @Autowired
    DataDateMapper dataDateMapper;
    @Autowired
    BaseDataMapper baseDataMapper;
    @Autowired
    ViewACTMapper viewACTMapper;

    @Override
    //计算活动水平
    public ServerResponse calALParameter(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        baseDataMapper.deleteByUser(user.getId());
        dataDateMapper.deleteByUser(user.getId());
        sectorMapper.deleteByUser(user.getId());
        try{
            if(user == null){
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        List<ViewResult> viewResults = viewResultMapper.selectbyUserid(user.getId());
        for(ViewResult viewResult:viewResults){
            // 上传supsector部门
            Integer supid1;
            Sector sector = sectorMapper.accuSelect(viewResult.getResultSupsector(),viewResult.getResultSupsector(),null,user.getId());
            if(sector == null){
                Sector sector1 = new Sector();
                sector1.setSectorAb(viewResult.getResultSupsector());
                sector1.setSectorName(viewResult.getResultSupsector());
                sector1.setUserId(viewResult.getResultUser());
                sector1.setCreateTime(viewResult.getResultCreateTime());
                sectorMapper.insert(sector1);
                supid1 = sector1.getSectorId();
            }else{
                supid1 = sector.getSectorId();
            }
            // 上传sector部门
            Integer supid2;
            sector = sectorMapper.accuSelect(viewResult.getResultSector(),viewResult.getResultSector(),supid1,user.getId());
            if(sector == null){
                Sector sector2 = new Sector();
                sector2.setSectorAb(viewResult.getResultSector());
                sector2.setSectorName(viewResult.getResultSector());
                sector2.setSupSectorId(supid1);
                sector2.setUserId(viewResult.getResultUser());
                sector2.setCreateTime(viewResult.getResultCreateTime());
                sectorMapper.insert(sector2);
                supid2 = sector2.getSectorId();
            }else{
                supid2 = sector.getSectorId();
            }
            // 上传service部门
            Integer supid3;
            sector = sectorMapper.accuSelect(viewResult.getResultService(),viewResult.getResultService(),supid2,user.getId());
            if(sector == null){
                Sector sector3 = new Sector();
                sector3.setSectorAb(viewResult.getResultService());
                sector3.setSectorName(viewResult.getResultService());
                sector3.setSupSectorId(supid2);
                sector3.setUserId(viewResult.getResultUser());
                sector3.setCreateTime(viewResult.getResultCreateTime());
                sectorMapper.insert(sector3);
                supid3 = sector3.getSectorId();
            }else{
                supid3 = sector.getSectorId();
            }
            // 上传tech部门
            Integer supid4 = 0;
            sector = sectorMapper.accuSelect(viewResult.getResultTechnology(),viewResult.getResultTechnology(),supid3,user.getId());
            if(sector == null){
                Sector sector4 = new Sector();
                if(viewResult.getResultTechnology() != null) {
                    sector4.setSectorAb(viewResult.getResultTechnology());
                    sector4.setSectorName(viewResult.getResultTechnology());
                    sector4.setSupSectorId(supid3);
                    sector4.setUserId(viewResult.getResultUser());
                    sector4.setCreateTime(viewResult.getResultCreateTime());
                    sectorMapper.insert(sector4);
                    supid4 = sector4.getSectorId();
                }else{
                    supid4 = supid3;
                }
            }else {
                supid4 = sector.getSectorId();
            }
            //查日期
            Integer dateid = dataDateMapper.selectbyYear(viewResult.getResultYear(),user.getId());
            if(dateid == null){
                DataDate dataDate = new DataDate();
                dataDate.setYear(viewResult.getResultYear());
                dataDate.setUserId(viewResult.getResultUser());
                dataDate.setCreateTime(viewResult.getResultCreateTime());
                dataDateMapper.insert(dataDate);
                dateid = dataDate.getDateId();
            }
            // 查找地区编号
            Integer region = regionMappingMapper.selectTHURegionId(viewResult.getResultThuRegion(),user.getId());
            //
            BaseData baseData = new BaseData();
            baseData.setBaseRegionId(region);
            baseData.setBaseDateId(dateid);
            baseData.setBaseSectorId(supid4);
            baseData.setBaseScenario(viewResult.getResultScenario());
            baseData.setBaseFuel(viewResult.getResultFuel());
            baseData.setBaseTechParam(viewResult.getResultTechEnergyConsumption());
            baseData.setBaseUserId(user.getId());
            baseData.setBaseCreateTime(viewResult.getResultCreateTime());
           // if(baseDataMapper.selectByBasedata(baseData) == null) {
            baseDataMapper.insert(baseData);
           // }
        }
        return ServerResponse.createBySuccessMessage("计算活动水平成功");
    }
    @Override

    //精确查询活动水平
    public ServerResponse<PageInfo> selectAL(Integer page,Integer limit,String scenario,String date,String fuel,String region,
                                             String superSector,String sector,String service,String technology, HttpSession session) {
        PageHelper.startPage(page, limit);
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
        }
        Integer userId=user.getId();



        List<ViewACT> result=viewACTMapper.selectByCondition(date,region,superSector,sector,service,technology,scenario,fuel,userId);
        if(result.size() == 0){
            return ServerResponse.createByErrorMessage("不存在匹配选项");
        }
        PageInfo<ViewACT> pageResult = new PageInfo<>(result);
        return ServerResponse.createBySuccess("查询活动水平成功",pageResult);

    }
    public ServerResponse<PageInfo> selectAL3(Integer page,Integer limit,String scenario,String date,String fuel,String region,
                                             String superSector,String sector,String service,String technology, HttpSession session) {
        PageHelper.startPage(page, limit);
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
        }
        int dateid = dataDateMapper.selectbyYear(date, user.getId());
        int regionid = regionMappingMapper.selectByRegionName(region, user.getId());
        int userId = user.getId();

        List<Integer> sectorids = new ArrayList<>();
        List<Float> avs = new ArrayList<>();
        List<ViewACT> result = new ArrayList<>();
        Integer supSectorId = sectorMapper.selectBySecName(superSector, user.getId());//获取顶级部门id
        List<Sector> sectors = sectorMapper.selectBySupsectorid(supSectorId, user.getId());//得到所有满足条件的二级部门
        if (superSector.length() <= 0) {

        }else{
        if (technology.length() <= 0) {
            if (service.length() <= 0) {
                if (sector.length() <= 0) {
                    if (superSector.length() <= 0) {
                        //四个部门值全为空
                        return ServerResponse.createByErrorMessage("部门信息不能全为空");
                    } else {
                        //只有顶级部门有传入值
                        for (Sector sec : sectors) {

                            List<Sector> services = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                            for (Sector ser : services) {
                                List<Sector> technologys = sectorMapper.selectBySupsectorid(ser.getSectorId(), user.getId());
                                for (Sector tech : technologys) {
                                    ViewACT view = new ViewACT();
                                    view.setRegion(region);
                                    view.setSupsector(superSector);
                                    view.setFuel(fuel);
                                    view.setScenario(scenario);
                                    view.setDate(date);
                                    view.setSector(sec.getSectorName());
                                    view.setService(ser.getSectorName());
                                    view.setTechnology(tech.getSectorName());
                                    List<Float> techParams = baseDataMapper.selectbyConditions(regionid, dateid, tech.getSectorId(), scenario, fuel, userId);
                                    for (Float f : techParams) {
                                        view.setTechParam(f);
                                        result.add(view);
                                    }
                                }
                            }
                        }

                    }
                } else {
                    //二级部门有传入值，三四级部门为空
                    Integer sectorId = 0;
                    for (Sector sec : sectors) {
                        if (sector.equals(sec.getSectorName())) {
                            sectorId = sec.getSectorId();
                        }
                    }
                    if (sectorId != 0) {
                        List<Sector> services = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的三级部门

                        for (Sector ser : services) {
                            List<Sector> technologys = sectorMapper.selectBySupsectorid(ser.getSectorId(), user.getId());
                            for (Sector tech : technologys) {
                                ViewACT view = new ViewACT();
                                view.setRegion(region);
                                view.setSupsector(superSector);
                                view.setFuel(fuel);
                                view.setScenario(scenario);
                                view.setDate(date);
                                view.setSector(sector);
                                view.setService(ser.getSectorName());
                                view.setTechnology(tech.getSectorName());
                                List<Float> techParams = baseDataMapper.selectbyConditions(regionid, dateid, tech.getSectorId(), scenario, fuel, userId);
                                for (Float f : techParams) {
                                    view.setTechParam(f);
                                    result.add(view);
                                }
                            }
                        }
                    } else {
                        return ServerResponse.createByErrorMessage("不存在匹配的二级部门");
                    }

                }


            } else {
                //三级部门有传入值
                Integer sectorId = 0;
                Integer serviceId = 0;
                for (Sector sec : sectors) {
                    if (sector.equals(sec.getSectorName())) {
                        sectorId = sec.getSectorId();
                        List<Sector> services = sectorMapper.selectBySupsectorid(sectorId, user.getId());
                        for (Sector ser : services) {
                            if (service.equals(ser.getSectorName())) {
                                serviceId = ser.getSectorId();
                            }

                        }
                    }

                }
                if (serviceId != 0) {
                    List<Sector> technologys = sectorMapper.selectBySupsectorid(serviceId, user.getId());//得到所有满足条件的四级部门
                    for (Sector tech : technologys) {
                        ViewACT view = new ViewACT();
                        view.setRegion(region);
                        view.setSupsector(superSector);
                        view.setFuel(fuel);
                        view.setScenario(scenario);
                        view.setDate(date);
                        view.setSector(sector);
                        view.setService(service);
                        view.setTechnology(tech.getSectorName());
                        List<Float> techParams = baseDataMapper.selectbyConditions(regionid, dateid, tech.getSectorId(), scenario, fuel, userId);
                        for (Float f : techParams) {
                            view.setTechParam(f);
                            result.add(view);
                        }
                    }
                } else {
                    return ServerResponse.createByErrorMessage("不存在匹配的三级部门");
                }


            }
        } else {
            Integer sectorId = 0;
            Integer serviceId = 0;
            Integer techId = 0;
            for (Sector sec : sectors) {
                if (sector.equals(sec.getSectorName())) {
                    sectorId = sec.getSectorId();
                    List<Sector> services = sectorMapper.selectBySupsectorid(sectorId, user.getId());
                    for (Sector ser : services) {
                        if (service.equals(ser.getSectorName())) {
                            serviceId = ser.getSectorId();
                            List<Sector> technologys = sectorMapper.selectBySupsectorid(serviceId, user.getId());
                            for (Sector tech : technologys) {
                                if (technology.equals(tech.getSectorName())) {
                                    ViewACT view = new ViewACT();
                                    view.setRegion(region);
                                    view.setSupsector(superSector);
                                    view.setFuel(fuel);
                                    view.setScenario(scenario);
                                    view.setDate(date);
                                    view.setSector(sector);
                                    view.setService(service);
                                    view.setTechnology(technology);
                                    List<Float> techParams = baseDataMapper.selectbyConditions(regionid, dateid, tech.getSectorId(), scenario, fuel, userId);
                                    for (Float f : techParams) {
                                        view.setTechParam(f);
                                        result.add(view);
                                    }
                                }
                            }
                        }

                    }
                }

            }
        }
    }
        if(result.size() == 0){
           return ServerResponse.createByErrorMessage("不存在匹配选项");
        }
        PageInfo<ViewACT> pageResult = new PageInfo<>(result);
        return ServerResponse.createBySuccess("查询活动水平成功",pageResult);
    }
   //按部门展示活动水平
    public ServerResponse<PageInfo> selectAL2(Integer page,Integer limit,String scenario,String date,String fuel,String region,String superSector,String sector,String service,String technology, HttpSession session){
        PageHelper.startPage(page, limit);
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
        }
        int dateid = dataDateMapper.selectbyYear(date,user.getId());
        int regionid = regionMappingMapper.selectByRegionName(region,user.getId());
        Integer sectorId = 0;
        List<Integer> sectorids = new ArrayList<>();
        List<Float> avs = new ArrayList<>();
        List<ViewACT> result = new ArrayList<>();

        if(technology.length() <= 0 ){
            if(service.length() <= 0){
                if(sector.length() <= 0){
                    if(superSector.length() <= 0){
                        //四个部门值全为空
                        return ServerResponse.createByErrorMessage("部门信息不能全为空");
                    }else {
                        //只有顶级部门有传入值
                        sectorId = sectorMapper.selectBySecName(superSector, user.getId());//获取顶级部门id
                        List<Sector> secordsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的二级部门
                        List<Sector> thirdsecs = new ArrayList<>();
                        for (Sector sec : secordsecs) {
                            List<Sector> thirdsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                            thirdsecs.addAll(thirdsec);//得到所有满足条件的三级部门
                        }
                        List<Sector> fourthsecs = new ArrayList<>();
                        for (Sector sec : thirdsecs) {
                            List<Sector> fourthsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                            fourthsecs.addAll(fourthsec);//得到所有满足条件的四级部门
                        }
                        for (Sector four : fourthsecs) {
                            sectorids.add(four.getSectorId());
                        }
                        // Map<Float,Integer> map = new HashMap();
                        for (Integer id : sectorids) {
                            avs = baseDataMapper.selectbyCondition(regionid, dateid, id, scenario, fuel);
                            if (avs.size() == 0) {
                                continue;
                            } else {
                                for (int i = 0; i < avs.size(); i++) {
                                    Integer secid = id;
                                    technology = sectorMapper.selectName(secid, user.getId());
                                    Integer serviceid = sectorMapper.selectIDtoSup(secid, user.getId());
                                    service = sectorMapper.selectName(serviceid, user.getId());
                                    Integer sector_id = sectorMapper.selectIDtoSup(serviceid, user.getId());
                                    sector = sectorMapper.selectName(sector_id, user.getId());
                                    ViewACT view = new ViewACT();
                                    view.setDate(date);
                                    view.setFuel(fuel);
                                    view.setRegion(region);
                                    view.setScenario(scenario);
                                    view.setSector(sector);
                                    view.setService(service);
                                    view.setSupsector(superSector);
                                    view.setTechnology(technology);
                                    view.setTechParam(avs.get(i));
                                    result.add(view);
                                }
                            }
                        }
                    }
                }else{
                    //二级部门有传入值，三四级部门为空
                    sectorId = sectorMapper.selectBySecName(sector, user.getId());//获取二级部门id
                    List<Sector> thirdsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的三级部门
                    List<Sector> foursecs = new ArrayList<>();
                    for (Sector sec : thirdsecs) {
                        List<Sector> foursec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                        foursecs.addAll(foursec);//得到所有满足条件的四级部门
                    }
                    //所有部门都有值
                    for(Sector four : foursecs) {
                        sectorids.add(four.getSectorId());
                    }
                    //avs = baseDataMapper.selectbyConditionBulk(regionid, dateid, sectorids, scenario, fuel);
                    for(Integer id:sectorids) {
                        avs = baseDataMapper.selectbyCondition(regionid, dateid, id, scenario, fuel);
                        if (avs.size() == 0) {
                            continue;
                        } else {
                            for (int i = 0; i < avs.size(); i++) {
                                Integer secid = id;
                                technology = sectorMapper.selectName(secid, user.getId());
                                Integer serviceid = sectorMapper.selectIDtoSup(secid, user.getId());
                                service = sectorMapper.selectName(serviceid, user.getId());
                                Integer sector_id = sectorMapper.selectIDtoSup(serviceid, user.getId());
                                sector = sectorMapper.selectName(sector_id, user.getId());
                                ViewACT view = new ViewACT();
                                view.setDate(date);
                                view.setFuel(fuel);
                                view.setRegion(region);
                                view.setScenario(scenario);
                                view.setSector(sector);
                                view.setService(service);
                                view.setSupsector(superSector);
                                view.setTechnology(technology);
                                view.setTechParam(avs.get(i));
                                result.add(view);
                            }
                        }
                    }
                }
            }else {
                //三级部门有传入值
                sectorId = sectorMapper.selectBySecName(service, user.getId());
                List<Sector> foursecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的四级部门
                if (foursecs.size() != 0) {
                    for (Sector four : foursecs) {
                        sectorids.add(four.getSectorId());
                    }
                    //avs = baseDataMapper.selectbyConditionBulk(regionid, dateid, sectorids, scenario, fuel);
                    for (Integer id : sectorids) {
                        avs = baseDataMapper.selectbyCondition(regionid, dateid, id, scenario, fuel);
                        if (avs.size() == 0) {
                            continue;
                        } else {
                            for (int i = 0; i < avs.size(); i++) {
                                Integer secid = id;
                                technology = sectorMapper.selectName(secid, user.getId());
                                Integer serviceid = sectorMapper.selectIDtoSup(secid, user.getId());
                                service = sectorMapper.selectName(serviceid, user.getId());
                                Integer sector_id = sectorMapper.selectIDtoSup(serviceid, user.getId());
                                sector = sectorMapper.selectName(sector_id, user.getId());
                                ViewACT view = new ViewACT();
                                view.setDate(date);
                                view.setFuel(fuel);
                                view.setRegion(region);
                                view.setScenario(scenario);
                                view.setSector(sector);
                                view.setService(service);
                                view.setSupsector(superSector);
                                view.setTechnology(technology);
                                view.setTechParam(avs.get(i));
                                result.add(view);
                            }
                        }
                    }
                } else {
                    //四级部门为空的情况
                    avs = baseDataMapper.selectbyCondition(regionid, dateid, sectorId, scenario, fuel);
                    for (Float av : avs) {
                        ViewACT view = new ViewACT();
                        view.setDate(date);
                        view.setFuel(fuel);
                        view.setRegion(region);
                        view.setScenario(scenario);
                        view.setSector(sector);
                        view.setService(service);
                        view.setSupsector(superSector);
                        view.setTechnology(technology);
                        view.setTechParam(av);
                        result.add(view);
                    }
                }
            }
        }else{
            sectorId = sectorMapper.selectBySecName(technology,user.getId());
            avs = baseDataMapper.selectbyCondition(regionid, dateid, sectorId, scenario, fuel);
            if(avs.size() == 0){
                return ServerResponse.createByErrorMessage("不存在匹配选项");
            }else {
                for (Float av : avs) {
                    ViewACT view = new ViewACT();
                    view.setDate(date);
                    view.setFuel(fuel);
                    view.setRegion(region);
                    view.setScenario(scenario);
                    view.setSector(sector);
                    view.setService(service);
                    view.setSupsector(superSector);
                    view.setTechnology(technology);
                    view.setTechParam(av);
                    result.add(view);
                }
            }
        }
        if(result.size() == 0){
            return ServerResponse.createByErrorMessage("不存在匹配选项");
        }
        PageInfo<ViewACT> pageResult = new PageInfo<>(result);
        return ServerResponse.createBySuccess("查询活动水平成功",pageResult);
    }

    @Override
    public ServerResponse CalculateSum(HttpSession session, String Sector,String date, String scenario,String fuel,String region) {
        JSONObject result =new JSONObject();
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        Integer supId=sectorMapper.selectBySecName(Sector,user.getId());
        Integer dateId=dataDateMapper.selectbyYear(date,user.getId());
        Integer regionId=regionMappingMapper.selectByRegionName(region,user.getId());

        List<Sector> subSector=sectorMapper.selectBySup(supId,user.getId());

        for(Sector sector:subSector){
            float sum=0;
            if(sectorMapper.selectRepeatSup(sector.getSectorId(),user.getId())<=0){
                BaseData baseData=new BaseData();
                baseData.setBaseFuel(fuel);
                baseData.setBaseDateId(dateId);
                baseData.setBaseRegionId(regionId);
                baseData.setBaseUserId(user.getId());
                baseData.setBaseScenario(scenario);
                baseData.setBaseSectorId(sector.getSectorId());
                //查询条件
                //result.put(sector.getSectorName(),baseDataMapper.selectSum(baseData));
                if(baseDataMapper.selectSum(baseData)!=null) {
                    sum = sum + baseDataMapper.selectSum(baseData);
                }
            }else{
                List<Sector> technology=sectorMapper.selectBySup(sector.getSectorId(),user.getId());

                for(Sector sector2:technology){

                    if(sectorMapper.selectRepeatSup(sector2.getSectorId(),user.getId())<=0){
                        BaseData baseData=new BaseData();
                        baseData.setBaseFuel(fuel);
                        baseData.setBaseDateId(dateId);
                        baseData.setBaseRegionId(regionId);
                        baseData.setBaseUserId(user.getId());
                        baseData.setBaseScenario(scenario);
                        baseData.setBaseSectorId(sector2.getSectorId());
                        //查询条件
                        if(baseDataMapper.selectSum(baseData)!=null) {
                            sum = sum + baseDataMapper.selectSum(baseData);
                        }
                    }else{
                        List<Sector> service=sectorMapper.selectBySup(sector2.getSectorId(),user.getId());
                        for(Sector sector3:service){
                            BaseData baseData=new BaseData();
                            baseData.setBaseFuel(fuel);
                            baseData.setBaseDateId(dateId);
                            baseData.setBaseRegionId(regionId);
                            baseData.setBaseUserId(user.getId());
                            baseData.setBaseScenario(scenario);
                            baseData.setBaseSectorId(sector3.getSectorId());
                            //查询条件

                            if(baseDataMapper.selectSum(baseData)!=null) {
                                sum = sum + baseDataMapper.selectSum(baseData);
                            }

                        }
                    }



                }//sector2
            }

            result.put(sector.getSectorName(),sum);
        }


        return ServerResponse.createBySuccess("部门求和成功",result);
    }
    @Override
    //按地区展示活动水平
    public ServerResponse ALSumByRegion(HttpSession session,String scenario,String date,String fuel,String superSector,String sector,String service,String technology) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        try {
            if (user == null) {
                return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int dateid = dataDateMapper.selectbyYear(date, user.getId());
        List<Integer> regionids = regionMappingMapper.selectAll(user.getId());
        List<ViewACT> views = new ArrayList<>();
        for(Integer regionid:regionids) {
            Integer sectorId = 0;
            List<Integer> sectorids = new ArrayList<>();
            Float avs = new Float(0);
            if (technology.length() <= 0) {
                if (service.length() <= 0) {
                    if (sector.length() <= 0) {
                        if (superSector.length() <= 0) {
                            //四个部门值全为空
                            return ServerResponse.createByErrorMessage("部门信息不能全为空");
                        } else {
                            //只有顶级部门有传入值
                            sectorId = sectorMapper.selectBySecName(superSector, user.getId());//获取顶级部门id
                            List<Sector> secordsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的二级部门
                            List<Sector> thirdsecs = new ArrayList<>();
                            for (Sector sec : secordsecs) {
                                List<Sector> thirdsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                                thirdsecs.addAll(thirdsec);//得到所有满足条件的三级部门
                            }
                            List<Sector> fourthsecs = new ArrayList<>();
                            for (Sector sec : thirdsecs) {
                                List<Sector> fourthsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                                fourthsecs.addAll(fourthsec);//得到所有满足条件的四级部门
                            }
                            for (Sector four : fourthsecs) {
                                sectorids.add(four.getSectorId());
                            }
                            avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                        }
                    } else {
                        //二级部门有传入值，三四级部门为空
                        sectorId = sectorMapper.selectBySecName(sector, user.getId());//获取二级部门id
                        List<Sector> thirdsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的三级部门
                        List<Sector> foursecs = new ArrayList<>();
                        for (Sector sec : thirdsecs) {
                            List<Sector> foursec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                            foursecs.addAll(foursec);//得到所有满足条件的四级部门
                        }
                        //所有部门都有值
                        for (Sector four : foursecs) {
                            sectorids.add(four.getSectorId());
                        }
                        avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                    }
                } else {
                    //三级部门有传入值
                    sectorId = sectorMapper.selectBySecName(service, user.getId());
                    List<Sector> foursecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的四级部门
                    if (foursecs.size() != 0) {
                        for (Sector four : foursecs) {
                            sectorids.add(four.getSectorId());
                        }
                        avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                    } else {
                        //四级部门为空的情况
                        avs = baseDataMapper.sumByregion2(dateid, sectorId, scenario, fuel, user.getId(),regionid);
                    }
                }
            } else {
                sectorId = sectorMapper.selectBySecName(technology, user.getId());
                avs = baseDataMapper.sumByregion2(dateid, sectorId, scenario, fuel, user.getId(),regionid);

            }
            ViewACT view = new ViewACT();
            String regionName = regionMappingMapper.IdToName(regionid,user.getId());
            view.setDate(date);
            view.setRegion(regionName);
            view.setFuel(fuel);
            view.setScenario(scenario);
            view.setSector(sector);
            view.setService(service);
            view.setSupsector(superSector);
            view.setTechnology(technology);
            view.setTechParam(avs);
            views.add(view);
        }

        return ServerResponse.createBySuccess("查询所有地区活动水平成功", views);

    }

    //按时间范围展示活动水平
    @Override
    public  ServerResponse SumByTime(String scenario, String start_year, String end_year,String fuel,
                                     String region, String superSector, String sector,
                                     String service, String technology, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("用户未登录,请先登录账户");
        }

        Integer regionid = regionMappingMapper.selectByRegionName(region,user.getId());
        List<Integer> dateids = dataDateMapper.selectbyRange(start_year,end_year,user.getId());
        List<ViewACT> views = new ArrayList<>();
        for (Integer dateid:dateids){
            Integer sectorId = 0;
            List<Integer> sectorids = new ArrayList<>();
            Float avs = new Float(0);
            if (technology.length() <= 0 ) {
                if (service.length() <= 0) {
                    if (sector.length() <= 0) {
                        if (superSector.length() <= 0) {
                            //四个部门值全为空
                            return ServerResponse.createByErrorMessage("部门信息不能全为空");
                        } else {
                            //只有顶级部门有传入值
                            sectorId = sectorMapper.selectBySecName(superSector, user.getId());//获取顶级部门id
                            List<Sector> secordsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的二级部门
                            List<Sector> thirdsecs = new ArrayList<>();
                            for (Sector sec : secordsecs) {
                                List<Sector> thirdsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                                thirdsecs.addAll(thirdsec);//得到所有满足条件的三级部门
                            }
                            List<Sector> fourthsecs = new ArrayList<>();
                            for (Sector sec : thirdsecs) {
                                List<Sector> fourthsec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                                fourthsecs.addAll(fourthsec);//得到所有满足条件的四级部门
                            }
                            for (Sector four : fourthsecs) {
                                sectorids.add(four.getSectorId());
                            }
                            avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                        }
                    } else {
                        //二级部门有传入值，三四级部门为空
                        sectorId = sectorMapper.selectBySecName(sector, user.getId());//获取二级部门id
                        List<Sector> thirdsecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的三级部门
                        List<Sector> foursecs = new ArrayList<>();
                        for (Sector sec : thirdsecs) {
                            List<Sector> foursec = sectorMapper.selectBySupsectorid(sec.getSectorId(), user.getId());
                            foursecs.addAll(foursec);//得到所有满足条件的四级部门
                        }
                        //所有部门都有值
                        for (Sector four : foursecs) {
                            sectorids.add(four.getSectorId());
                        }
                        avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                    }
                } else {
                    //三级部门有传入值
                    sectorId = sectorMapper.selectBySecName(service, user.getId());
                    List<Sector> foursecs = sectorMapper.selectBySupsectorid(sectorId, user.getId());//得到所有满足条件的四级部门
                    if (foursecs.size() != 0) {
                        for (Sector four : foursecs) {
                            sectorids.add(four.getSectorId());
                        }
                        avs = baseDataMapper.sumByregion(dateid, sectorids, scenario, fuel, user.getId(),regionid);
                    } else {
                        //四级部门为空的情况
                        avs = baseDataMapper.sumByregion2(dateid, sectorId, scenario, fuel, user.getId(),regionid);
                    }
                }
            }else{
                sectorId = sectorMapper.selectBySecName(technology, user.getId());
                avs = baseDataMapper.sumByregion2(dateid, sectorId, scenario, fuel, user.getId(),regionid);

            }
            ViewACT view = new ViewACT();
            String year = dataDateMapper.selectbyYearId(dateid,user.getId());
            view.setDate(year);
            view.setTechParam(avs);
            views.add(view);

        }
        return ServerResponse.createBySuccess("时间范围内活动水平结果查询成功",views);
    }




}
